package demo.gojapn.translationdemo;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import demo.gojapn.translationdemo.common.view.PinnedSectionListView;
import demo.gojapn.translationdemo.common.view.ProgressWheel;
import demo.gojapn.translationdemo.common.view.view.indicator.IndicatorViewPager;
import demo.gojapn.translationdemo.common.view.view.indicator.ScrollIndicatorView;
import demo.gojapn.translationdemo.common.view.view.indicator.slidebar.LayoutBar;
import demo.gojapn.translationdemo.common.view.view.indicator.slidebar.ScrollBar;
import demo.gojapn.translationdemo.entity.BaseEntity;
import demo.gojapn.translationdemo.entity.Segmentation;
import demo.gojapn.translationdemo.entity.TransFormItem;

public class MainActivity extends AppCompatActivity {

    private  ScrollIndicatorView scrollIndicatorView;
    private PinnerListViewAdapter adapter;
    private  MediaPlayerService service;
    private ProgressWheel wheel;
    private ImageView imgIn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init_tabs();
        init_data();
        service = new MediaPlayerService(this, R.raw.default_alarm);

    }

    private void init_tabs() {
        String[] arrays = getResources().getStringArray(R.array.lsit_trans);
        scrollIndicatorView = ((ScrollIndicatorView) findViewById(R.id.tabs));
        scrollIndicatorView.setScrollBar(new LayoutBar(this,R.layout.layout_slidebar, ScrollBar.Gravity.CENTENT_BACKGROUND));
        //  PinnedSectionListView listView  = ((PinnedSectionListView) findViewById(R.id.listview));
        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        IndicatorViewPager indicatorViewPager = new IndicatorViewPager(scrollIndicatorView,pager);
        indicatorViewPager.setAdapter(new MyAdapter(arrays));
        indicatorViewPager.setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() {
            @Override
            public void onIndicatorPageChange(int preItem, int currentItem) {
                TextView textView = (TextView) scrollIndicatorView.getItemView(currentItem);
                textView.setTextColor(getResources().getColor(R.color.colorAccent));
                textView = (TextView) scrollIndicatorView.getItemView(preItem);
                textView.setTextColor(getResources().getColor(android.R.color.white));
            }
        });
        TextView textView = (TextView) scrollIndicatorView.getItemView(indicatorViewPager.getCurrentItem());
        textView.setTextColor(getResources().getColor(R.color.colorAccent));
    }

    private void init_data() {
        List<BaseEntity> entities = new ArrayList<>();
        String[] items_1 = getResources().getStringArray(R.array.lsit_trans_items_1);
        String[] items_2 = getResources().getStringArray(R.array.lsit_trans_items_2);
        String[] items_3 = getResources().getStringArray(R.array.lsit_trans_items_3);
        entities.add(new Segmentation("初次见面"));
        for (String s:items_1){
            entities.add(new TransFormItem(s));
        }
        entities.add(new Segmentation("见boss"));
        for (String s:items_2){
            entities.add(new TransFormItem(s));
        }
        entities.add(new Segmentation("就餐"));
        for (String s:items_3){
            entities.add(new TransFormItem(s));
        }
        adapter  =  new PinnerListViewAdapter(this,entities);
    }


    @Override
    public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onPostCreate(savedInstanceState, persistentState);
    }

    private  class MyAdapter extends IndicatorViewPager.IndicatorViewPagerAdapter {

        private LayoutInflater inflate =LayoutInflater.from(getApplicationContext());
        private String[] arrays;


        private MyAdapter(String[] arrays){
           this.arrays = arrays;
        }

        @Override
        public int getCount() {
            return arrays.length;
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView  ==null){
                convertView  = inflate.inflate(R.layout.text_slide,null);
            }
            TextView textView = (TextView) convertView;
            textView.setText(arrays[position]);
            return convertView;
        }

        @Override
        public View getViewForPage(int position, View convertView, ViewGroup container) {
            if (convertView == null){
                convertView  = inflate.inflate(R.layout.tab_content, null);
            }
            final PinnedSectionListView listView = (PinnedSectionListView) convertView;
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    BaseEntity entity  = (BaseEntity) parent.getAdapter().getItem(position);
                    if (entity.getType() == BaseEntity.Type.TransSep) return;
                    showPopupView(entity,listView);
                }
            });
            listView.setAdapter(adapter);
            return convertView;
        }
    }


    private void showPopupView(BaseEntity entity,ListView listView) {

        View view = View.inflate(this,R.layout.pop_view,null);
        wheel = (ProgressWheel) view.findViewById(R.id.progress_wheel);
        service.setWheel(wheel);
        imgIn = (ImageView) view.findViewById(R.id.progress_wheel_src_in);
        TextView textView1 = (TextView) view.findViewById(R.id.text1);
        textView1.setText(entity.getContent_ch());
        textView1 = (TextView) view.findViewById(R.id.text2);
        textView1.setText("ｗｄヲ「ｐｑｄｋくぇｄ");

        PopupWindow pop = new PopupWindow(this);
        pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        pop.setHeight(dip2px(this,250));
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setFocusable(true);
        pop.setContentView(view);
        pop.showAtLocation(listView,Gravity.BOTTOM, 0, 0 );
    }
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public  void  mc_play(View v){

        service.playOrPause();
    }

}
